package com.company.base.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class StringUtil {
	
	public static boolean isEmpty(String text){		
		return (text == null || text.trim().length() == 0);
	}
	
	public static boolean isNumeric(String str){ 
		Pattern pattern = Pattern.compile("[0-9]*"); 
		return pattern.matcher(str).matches(); 
	}
	
	public static String filter(String str){
		if(!isEmpty(str)){
			Pattern p = Pattern.compile("(?i)[^a-zA-Z0-9\u4E00-\u9FA5]");
			Matcher m = p.matcher(str);
			str = m.replaceAll(""); 
			return str;
		}else{
			return null;
		}
	}
	
	public String getRemortIP(HttpServletRequest request) {
	    if (request.getHeader("x-forwarded-for") == null) {
	        return request.getRemoteAddr();
	    }
	    return request.getHeader("x-forwarded-for");
	}

	public String getIpAddr(HttpServletRequest request) {
	    String ip = request.getHeader("x-forwarded-for");
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getHeader("Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getHeader("WL-Proxy-Client-IP");
	    }
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
	        ip = request.getRemoteAddr();
	    }
	    return ip;
	}
}
